/**
 * @author qiuxj
 * @text 分公司管理
 */
var Company = {
    init: function(){
        // DWR方法
        var dwr = {
        	getChildCompany: companyDwr.getChildCompany,
        	getCompany: companyDwr.getCompany,
        	createCompany: companyDwr.createCompany,
        	updateCompany: companyDwr.updateCompany
        };
        
        // 增加按钮
        var addBtn = new CQCIS.Button({
            text: '增加 (<u>A</u>)',
            iconCls: 'btn_insert',
            handler: function(){
                var checkMenu = companyTree.getSelectionModel().getSelectedNode();
                if (checkMenu && checkMenu.getDepth() < 4) {
                    companyWin.show();
                    companyWin.form.findField("upperCompanyId").setValue(checkMenu.id);
                } else if (checkMenu && checkMenu.getDepth() > 3) {
                    CQCIS.msg("不能在选定地方添加！");
                } else {
                    CQCIS.msg("请选择一个分公司！");
                }
            }
        });
        
        // 修改按钮
        var editBtn = new CQCIS.Button({
            text: '修改 (<u>E</u>)',
            iconCls: 'btn_modify',
            handler: function(){
                var checkMenu = companyTree.getSelectionModel().getSelectedNode();
                if (checkMenu) {
                    companyWin.load(checkMenu.id);
                } else {
                    CQCIS.msg("请选择一个分公司！");
                }
                
            }
        });
        
        // 菜单字段对象
        var companyMeta = [{
            id: 'companyId',
            field: 'companyId',
            desc: '分公司ID',
            xtype: 'hidden'
        }, {
            field: 'upperCompanyId',
            desc: '上级分公司ID',
            xtype: 'hidden'
        }, {
            field: 'companyName',
            desc: '分公司名称',
            vtype: 'valText',
            obj: {
                emptyText: '名称只能由中文、英文或数字组成',
                allowBlank: false
            }
        }, {
            field: 'companyCode',
            desc: '分公司编码',
            obj: {
                value: '000'
            }
        }, {
            field: 'state',
            desc: '状态',
            xtype: 'acombo',
            obj: {
                ds: {
                    data: [[0, '无效'], [1, '有效']],
                    fields: ['key', 'label']
                },
                value: 1
            }
        }, {
            field: 'attr',
            desc: '属性串',
            obj: {
                value: '00000000000000000000'
            }
        }];
        
        // 菜单信息窗口
        var companyWin = new CQCIS.form.WinForm({
            title: '分公司信息',
            modal: true,
            width: 340,
            height: 200,
            items: new CQCIS.form.DWRForm({
                dwr: {
                    load: dwr.getCompany,
                    save: dwr.createCompany,
                    update: dwr.updateCompany
                },
                labelWidth: 70,
                metas: companyMeta,
                fieldWidth: 220,
                listeners: {
                    'saveafter': function(msg){
                        CQCIS.msg("操作成功！");
                        var checkMenu = companyTree.getSelectionModel().getSelectedNode();
                        var node = checkMenu.parentNode ? checkMenu.parentNode : checkMenu;
                        companyTree.loader.load(node, function(){
                            if (!node.isExpanded()) {
                                node.expand();
                            }
                        });
                    }
                }
            })
        });
        
        var addMenu = new Ext.menu.Item({
            iconCls: 'menu_insert',
            id: 'addMenu',
            text: '增加',
            handler: addBtn.handler
        });
        var editMenu = new Ext.menu.Item({
            iconCls: 'menu_modify',
            id: 'editMenu',
            text: '修改',
            handler: editBtn.handler
        });
        
        var treeMenu = new Ext.menu.Menu({
            id: 'mainMenu',
            items: [addMenu, editMenu]
        });
        
        // 菜单树
        var companyTree = new CQCIS.tree.ColumnTree({
            rootId: CQCIS.SM.COMPANY_CQ_NODEID,
            rootText: '重庆局',
            dwrCall: dwr.getChildCompany,
//            nodeMeta: {
//                leaf: 'menuIdend'
//            },
            tbar: ['-', addBtn, editBtn],
            selModel: 'o',
            columns: [{
                id: 'companyId',
                header: '分公司名称',
                width: 250,
                dataIndex: 'companyName'
            }, {
                header: '分公司编码',
                width: 250,
                dataIndex: 'companyCode'
            }, {
                header: '上级分公司ID',
                width: 200,
                dataIndex: 'upperCompanyId'
            }, {
                header: '状态',
                width: 80,
                dataIndex: 'state',
                renderer: function menuFlag(value){
                    var str = "";
                    switch (value) {
                        case 0:
                            str = "无效";
                            break;
                        case 1:
                            str = "有效";
                            break;
                        default:
                            str = "有效";
                            break;
                    }
                    return str;
                }
            }, {
                header: '属性串',
                width: 80,
                dataIndex: 'attr'
            }],
            listeners: {
                'contextmenu': function(node, e){
                    treeMenu.showAt(e.getXY());
                }
            }
        });
        
        // 渲染对象
        companyTree.render();
    }
};

Ext.onReady(Company.init, Company);
